home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Libraries / GrafSys 2.0 / Demos / Orbit ƒ / orbit.p next >
Encoding:
Text File  |  1993-07-04  |  4.8 KB  |  192 lines  |  [TEXT/PJMM]

  1. program Orbits;
  2.  
  3. { demo program for grafsys, © 1993 by C. Franz }
  4. {}
  5. { this demo demonstrates inheritance and arbitrary rotation }
  6.  
  7. {this demo program shows three spheres that orbit each other. They are }
  8. {essentially three objects. Sun, Planet and moon are one object each       }
  9.  
  10.     uses
  11.         Matrix, Transformations, GrafSysCore, GrafSysScreen, GrafSysObject, Resources;
  12.  
  13.     const
  14.         degrees = 0.01745329; (* π/180 *)
  15.         cTheWindow = 400;
  16.  
  17.     type
  18.         SolarSystem = object
  19.                 sun: TSGenericObject3D;
  20.                 planet: TSGenericObject3D;
  21.                 moon: TSGenericObject3D;
  22.                 SunRad: real;
  23.                 PlanetRad: real;
  24.                 MoonRad: Real;
  25.                 frame: rect;
  26.                 procedure Init;
  27.                 procedure Draw;
  28.                 procedure Animate;
  29.             end;
  30.  
  31.     var
  32.         Sol: SolarSystem;
  33.  
  34. {CalcObjectFrame : calculates the rect that will contain the sphere         }
  35. {                        uses the same perspective projection that usually is    }
  36. {                        used by grafsys                                                }
  37. {     v             contains the (transformed) wc of the sphere's center            }
  38. {     radius     is the size of the shpere                                             }
  39. {     theRect    returns the rect used for drawing the sphere                     }
  40.  
  41.     procedure CalcObjectFrame (v: vector4; radius: real; var theRect: Rect; var z: real);
  42.  
  43.         var
  44.             zbyd: real;
  45.             x, y: real;
  46.             sx, sy, sr: integer;
  47.  
  48.     begin
  49.         GetVector4(v, x, y, z); (* get the sphere's z *)
  50.         ProjectPoint(v, sx, sy);(* use this as center for the rect *)
  51.         SetRect(theRect, sx, sy, sx, sy);
  52.  
  53.         zbyd := 1 / (z / current3DPort^.d + 1);
  54.         sr := Trunc(radius * zbyd); (* project the radius *)
  55.         InsetRect(theRect, -sr, -sr);
  56.     end;
  57.  
  58. { Methods }
  59.     procedure SolarSystem.Init;
  60.         var
  61.             thePass: TMatrixPass;
  62.             OK: LongInt;
  63.  
  64.     begin
  65.         New(Sun);
  66.         Sun.Init;
  67.         OK := Sun.AddPoint(0, 0, 0);
  68.         New(Planet);
  69.         Planet.Init;
  70.         OK := Planet.AddPoint(0, 0, 0);
  71.         New(Moon);
  72.         Moon.Init;
  73.         OK := Moon.AddPoint(0, 0, 0);
  74.         SetRect(frame, 32000, 32000, -32000, -32000); (* almost-minimum empty rect *)
  75.  
  76.     (* Now set up the radius *)
  77.         SunRad := 50;
  78.         PlanetRad := 20;
  79.         MoonRad := 10;
  80.  
  81.     (* now lets move the planets and moons where they belong *)
  82.         Planet.Translate(100, 0, 0);
  83.         Moon.Translate(30, 0, 0);
  84.  
  85.     (* Now lets make them inheritances *)
  86.         thePass := Sun.FFPassOn;
  87.         Planet.FFInherit(thePass);
  88.         thePass := Planet.FFPassOn;
  89.         Moon.FFInherit(thePass);
  90.     end;
  91.  
  92.     procedure SolarSystem.Draw;
  93.         var
  94.             sunR: rect;
  95.             planetR: Rect;
  96.             moonR: Rect;
  97.             sunZ, planetZ, moonZ: Real;
  98.             theVector: Vector4;
  99.             priority: array[1..3] of record
  100.                     theRect: rect;
  101.                     deepZ: real
  102.                 end;
  103.  
  104.     begin
  105. (* Step 1: transform the object and all the points in it *)
  106.         sun.transform(FALSE);
  107.         planet.transform(FALSE); (* not the order! *)
  108.         moon.transform(TRUE);
  109. (* Step 2 : access the data base, get the transformed point and *)
  110. (*              calculate the frame for the solar object. We use a little trick: *)
  111. (*              we always access the first point (buffer 0, offset 0 ) so we   *)
  112. (*              don't need to calculate an offset                                             *)
  113.         theVector := sun.theBufs[0]^[0].transformed;
  114.         CalcObjectFrame(theVector, sunRad, sunR, sunZ);
  115.         theVector := Planet.theBufs[0]^[0].transformed;
  116.         CalcObjectFrame(theVector, planetRad, planetR, planetZ);
  117.         theVector := Moon.theBufs[0]^[0].transformed;
  118.         CalcObjectFrame(theVector, moonRad, moonR, moonZ);
  119.  
  120. (* step 3: sort the rects in their z-priority *)
  121.         priority[1].theRect := sunR;
  122.         priority[1].deepZ := sunZ;
  123.  
  124.         if planetZ > Priority[1].deepZ then
  125.             begin
  126.                 priority[2] := priority[1];
  127.                 priority[1].theRect := planetR;
  128.                 priority[1].deepZ := planetZ;
  129.             end
  130.         else
  131.             begin
  132.                 priority[2].theRect := planetR;
  133.                 priority[2].deepZ := planetZ
  134.             end;
  135.  
  136.         if moonZ > Priority[1].deepZ then
  137.             begin
  138.                 priority[3] := priority[2];
  139.                 priority[2] := priority[1];
  140.                 priority[1].theRect := moonR;
  141.                 priority[1].deepZ := moonZ;
  142.             end
  143.         else if moonZ > Priority[2].deepZ then
  144.             begin
  145.                 priority[3] := priority[2];
  146.                 priority[2].theRect := moonR;
  147.                 priority[2].deepZ := moonZ;
  148.             end
  149.         else
  150.             begin
  151.                 priority[3].theRect := moonR;
  152.                 priority[3].deepZ := moonZ
  153.             end;
  154.  
  155. (* Step 4 : Erase frame and redraw *)
  156.         EraseRect(frame);
  157.         FrameOval(Priority[1].theRect);
  158.         EraseOval(Priority[2].theRect);
  159.         FrameOval(Priority[2].theRect);
  160.         EraseOval(Priority[3].theRect);
  161.         FrameOval(Priority[3].theRect);
  162.  
  163. (* Step 5: calc new frame *)
  164.         UnionRect(sunR, planetR, frame);
  165.         UnionRect(frame, moonR, frame);
  166.     end;
  167.  
  168.     procedure SolarSystem.Animate;
  169.     begin
  170.         Sun.Rotate(0 * degrees, 5 * degrees, 2 * degrees);
  171.         Planet.Rotate(0 * degrees, 5 * degrees, 0 * degrees);
  172.     end;
  173.  
  174.     var
  175.         EyeLoc: Vector4;
  176.         theWindow: WindowPtr;
  177.         dummyL: LongInt;
  178.  
  179. begin
  180.     InitGrafSys;
  181.     theWindow := GetNew3DWindow(cTheWindow, pointer(-1));
  182.     SetVector4(EyeLoc, 0, 0, -1);
  183.     SetEye(TRUE, EyeLoc, 0, 0, 0, 90 * degrees, none);
  184.     New(Sol);
  185.     Sol.Init;
  186.     Sol.Draw;
  187.     repeat
  188.         Sol.Animate;
  189.         Sol.Draw;
  190.         Delay(10, dummyL);
  191.     until button;
  192. end.